<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//json 输出



function jsonOut($code,$msg,$data=array()){
    echo json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data));
    exit();
}


//文件递归
function recursionCate($data,$key,$keyval,$pkey)
{
    $tree = '';
    $new = array();
    foreach ($data as $k => $v) {
        if ($v[$pkey] == $keyval) {
            $new['cate_id'] = $v['cate_id'];
            $new['cate_name'] = $v['cate_name'];
            $new[$pkey] = recursionCate($data, $key, $v[$key],$pkey);
            $tree[] = $new;
        }
    }
    return $tree;
}

//创建文件树菜单
function createCateTree($data,$key,$pkey,$name){
    $cate_id = session('cate_id');
    $tree = '<ul>';
    $jstree = '';
    foreach ($data as $k=>$v){
        if($v[$key] == $cate_id){
            $jstree = '{"selected":true}';
        }else{
            $jstree = '';
        }
        $tree .='<li class="tree_list jstree-open" data-id="'.$v[$key].'" data-jstree=\''.$jstree.'\'>'.$v[$name];
        if($v[$pkey] !=0){
            $tree .= createCateTree($v[$pkey],$key,$pkey,$name);
        }
        $tree .='</li>';
    }
    $tree .='</ul>';

    return $tree;
}

//库房递归
function recursionStorage($data,$key,$keyval,$pkey)
{
    $tree = '';
    $new = array();
    foreach ($data as $k => $v) {
        if ($v[$pkey] == $keyval) {
            $new['sto_id'] = $v['sto_id'];
            $new['sto_name'] = $v['sto_name'];
            $new[$pkey] = recursionStorage($data, $key, $v[$key],$pkey);
            $tree[] = $new;
        }
    }
    return $tree;
}

//库房树菜单
function createStorageTree($data,$key,$pkey,$name){
    $tree = '<ul>';
    $jstree = '';
    foreach ($data as $k=>$v){
            $jstree = '';
        $tree .='<li class=" tree_list jstree-open" data-id="'.$v[$key].'" data-jstree=\''.$jstree.'\'>'.$v[$name];
        if($v[$pkey] !=0){
            $tree .= createStorageTree($v[$pkey],$key,$pkey,$name);
        }
        $tree .='</li>';
    }
    $tree .='</ul>';

    return $tree;
}

//查找数组的祖宗级
function getAncestors($data,$id,$pid){
    $str = '';
    if($data[$id]){
        $str .= $id.'-'.$data[$id]['cate_name'].',';
        if($data[$id]['cate_parent_id'] != $pid){
            $str .= getAncestors($data,$data[$id]['cate_parent_id'],$pid);
        }
    }
    return $str;
}

//将数组的指定的值变成键
function arrayValueToKey($arr = array(),$value='id'){
    if(!is_array($arr)){
        return false;
    }
    foreach($arr as $key=>$val){
        $new_arr[$val[$value]] = $val;
    }
    return $new_arr;
}

//返回对应的单位
function count_size($bit)
{
    $type = array('Bytes', 'KB', 'MB', 'GB', 'TB');
    for ($i = 0; $bit >= 1024; $i++)//单位每增大1024，则单位数组向后移动一位表示相应的单位
    {
        $bit /= 1024;
    }
    return (floor($bit * 100) / 100) . $type[$i];//floor是取整函数，为了防止出现一串的小数，这里取了两位小数
}

function recursionCate2($data,$key,$keyval,$pkey)
{
    $tree = '';
    $new = array();
    foreach ($data as $k => $v) {
        if ($v[$pkey] == $keyval) {
            $new['org_id'] = $v['org_id'];
            $new['org_name'] = $v['org_name'];
            $new[$pkey] = recursionCate2($data, $key, $v[$key],$pkey);
            $tree[] = $new;
        }
    }
    // dump($tree);die;
    return $tree;
}

//创建文件树菜单
function createCateTree2($data,$key,$pkey,$name){
    $org_id = session('org_id');
    $tree = '<ul>';
    $jstree = '';
    foreach ($data as $k=>$v){
        if($v[$key] == $org_id){
            $jstree = '{"selected":true}';
        }else{
            $jstree = '';
        }
        $tree .='<li class="tree_list jstree-open" data-id="'.$v[$key].'" data-jstree=\''.$jstree.'\'>'.$v[$name];
        if($v[$pkey] !=0){
            $tree .= createCateTree2($v[$pkey],$key,$pkey,$name);
        }
        $tree .='</li>';
    }
    $tree .='</ul>';

    return $tree;
}
